package com.gexin.controller.system.order;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.gexin.common.ExcelUtils;
import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.ExportDocBeetl;
import com.gexin.common.utils.IdGenerateUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.order.OrderInvoice;
import com.gexin.model.order.OrderInvoiceExcel;
import com.gexin.model.order.OrderListExcel;
import com.gexin.service.order.OrderInvoiceService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@ControllerBind(controllerKey = "/system/order/invoice", viewPath = "WEB-INF/system/order")
public class InvoiceController  extends SystemBaseController {

	public void index(){
		if (isAjax()){
			String id = getPara("id");
			Integer state = getParaToInt("state");
			String createTime = getPara("create_time");
			Page<OrderInvoice> page = OrderInvoiceService.dao.findList(getpage() , getpageSize(),id,state,createTime);
			renderJson(page);
			return;
		}
		render("order_invoice.html");
	}

	/**
	 * 导出excle
	 */
	public void downLoadExcle(){
		OrderInvoice o = new OrderInvoice();
		o.setState(OrderInvoice.STATE_SUBMIT);
		List<OrderInvoice> byOrderInvoice = OrderInvoice.dao.findByOrderInvoice(o);
		/*ExportDocBeetl export  = new  ExportDocBeetl("/excel/invoiceTemplate.xml");
		export.binding("list", byOrderInvoice);
		export.exportDocument(getResponse(), getRequest(), "发票清单");*/

		//黎峻修复兼容excel 03 07 更换导出方式
		List<OrderInvoiceExcel> excel_list =new LinkedList<>();
		for(OrderInvoice i:byOrderInvoice){
			OrderInvoiceExcel excel =new OrderInvoiceExcel();
			excel.setId(i.getId());
			excel.setEin(StringUtils.isEmpty(i.getEin())?"/":i.getEin());
			excel.setName(i.getType().intValue()==1?i.getUserName():i.getCompanyName());
			excel.setExpress_address(StringUtils.isEmpty(i.getStr("express_address"))?"/":i.getStr("express_address"));
			excel.setExpress_phone(StringUtils.isEmpty(i.getStr("express_phone"))?"/":i.getStr("express_phone"));
			excel.setBankName(StringUtils.isEmpty(i.getBankName())?"/":i.getBankName());
			excel.setBankNumber(StringUtils.isEmpty(i.getBankNumber())?"/":i.getBankNumber());
			excel.setKind(i.getType().intValue()==1?"个人":"企业");
			excel.setProduct_code("/");
			excel.setProduct_names(StringUtils.isEmpty(i.getStr("product_names"))?"/":i.getStr("product_names"));
			excel.setSku_names(StringUtils.isEmpty(i.getStr("sku_names"))?"/":i.getStr("sku_names"));
			excel.setProduct_tag(StringUtils.isEmpty(i.getStr("product_tag"))?"/":i.getStr("product_tag"));
			excel.setNums(StringUtils.isEmpty(i.getStr("nums"))?"/":i.getStr("nums"));
			excel.setUnit_price(StringUtils.isEmpty(i.getStr("unit_price"))?"/":i.getStr("unit_price"));
			excel.setPrice(StringUtils.isEmpty(i.getStr("price"))?"/":i.getStr("price"));
			excel.setRate("3%");
			excel_list.add(excel);
		}
		System.err.println(JsonKit.toJson(excel_list));
		try {
			Workbook workbook =  ExcelExportUtil.exportExcel(new ExportParams("发票清单",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"sheet1"), OrderInvoiceExcel.class, excel_list);
			getResponse().setHeader("Content-Disposition", "attachment; filename=" + IdGenerateUtils.getId()+ ".xls");
			workbook.write(getResponse().getOutputStream());
		}catch (Exception e){
			System.err.println("导出失败，"+e);
		}

		renderNull();
	}

	/**
	 * 修改状态
	 */
	public void changeStatus(){
		String id = getPara("id");
		if(StringUtil.isBlank(id)){
			fail("订单编号不能为空!");
			return;
		}
		OrderInvoice orderInvoice = OrderInvoice.dao.findById(id);
		if(orderInvoice==null){
			fail("订单不存在!");
			return;
		}
		if(OrderInvoice.STATE_SUBMIT.equals(orderInvoice.getState())){
			fail("发票单状态错误!状态修改失败!");
			return;
		}
		orderInvoice.setState(OrderInvoice.STATE_FINISH);
		orderInvoice.setOptionUser(getLoginUserId());
		orderInvoice.setMakeInvoiceTime(new Date());
		boolean update = orderInvoice.update();
		renderJson(new ApiResponse(update,update?"修改状态成功!":"修改状态失败!"));
	}

	/**
	 * 批量修改状态
	 */
	public void allChangeState(){
		String times = getPara("times");
		if(StringUtil.isBlank(times)){
			fail("请选择日期");
			return;
		}
		String[] split = times.split(" - ");
		if(split.length!=2){
			fail("时间格式错误!");
			return;
		}
		List<OrderInvoice> byCreateTime = OrderInvoice.dao.findByCreateTime(split);
		for (OrderInvoice orderInvoice : byCreateTime) {
			orderInvoice.setState(OrderInvoice.STATE_FINISH);
			orderInvoice.setOptionUser(getLoginUserId());
			orderInvoice.setMakeInvoiceTime(new Date());
			orderInvoice.update();
		}
		success("修改完成!");
	}

}
